<?php
namespace app\common\service;

use think\Db;
use think\Model;

class IndexService extends Model{

    /**
     * 获取--首页--轮播图
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_banner_info(){
        $data = Db::name('banner')->where('status','=',1)->select();
        return $data;
    }

    /**
     * 获取--首页--精选推荐
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_top_picks(){
        $where['status'] = 1;
        $where['shelf_status'] = 1;
        $where['top_picks'] = 1;
        $data = Db::name('product')->where($where)->order('orderby','ASC')->limit(4)->select();
        if(!$data){
            return [];
        }
        foreach($data as $k=>$v){
            $images_arr = explode(',',$v['images']);
            $data[$k]['image_top_picks'] = $images_arr[0];
        }
        return $data;
    }

    /**
     * 获取--首页--新品--默认选取至多8个
     * @param $cate_id
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_home_cate($cate_id){
        $where['status'] = 1;
        $where['shelf_status'] = 1;
        $where['category_id'] = $cate_id;
        $data = Db::name('product')->where($where)->order('orderby','ASC')->limit(16)->select();
        if(!$data){
            return [];
        }
        foreach($data as $k=>$v){
            $images_arr = explode(',',$v['images']);
            $data[$k]['image_main'] = $images_arr[0];
        }
        $data_new = [
            [$data[0],$data[1]],
            [$data[2],$data[3]],
            [$data[4],$data[5]],
            [$data[6],$data[7]],
//            [$data[8],$data[9]],
//            [$data[10],$data[11]],
//            [$data[12],$data[13]],
//            [$data[14],$data[15]],
        ];

//        dump($data_new);die;
        return $data_new;
    }


}
